﻿using Microsoft.AspNetCore.Mvc;
using mozhi.smarterp.Finance.AccountsPayables.PurchasePayables;
using mozhi.smarterp.Finance.AccountsPayables.PurchasePayables.Links;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;

namespace mozhi.smarterp.Finance.AccountsPayables
{
    [RemoteService]
    [Route("api/v1/accounts-payables")]
    public class AccountsPayableController : BaseController
    {
        private IPurchasePayableAppService _appService;

        private IPurchasePayableReceiptAppService _linkAppService;

        public AccountsPayableController(IPurchasePayableAppService appService, IPurchasePayableReceiptAppService linkAppService)
        {
            _appService = appService;
            _linkAppService = linkAppService;
        }

        [HttpGet]
        public async Task<PagedResultDto<PurchasePayableDto>> GetAsync(PurchasePayablePagedResultRequestDto filter)
        {
            var payables = await _appService.GetListAsync(filter);

            return payables;
        }

        [HttpGet]
        [Route("{id}")]
        public async Task<PurchasePayableDto> GetAsync(Guid id)
        {
            var payable = await _appService.GetAsync(id);
            return payable;
        }

        [HttpPost]
        public async Task<PurchasePayableDto> CreateAsync(PurchasePayableCreateDto input)
        {
            var result = await _appService.CreateAsync(input);

            return result;
        }

        [HttpPut]
        [Route("{id}")]
        public async Task<PurchasePayableDto> UpdateAsync(PurchasePayableUpdateDto input)
        {
            var result = await _appService.UpdateAsync(input);

            return result;
        }

        [HttpDelete]
        [Route("{id}")]
        public async Task DeleteAsync(Guid id)
        {
            await _appService.DeleteAsync(id);
        }

        [HttpPost]
        [Route("{id}/payments")]
        public async Task PushPaymentAsync(Guid id)
        {
            await _appService.PushPaymentAsync(id);
        }

        [HttpGet]
        [Route("purchase-receipts/{receiptId}")]
        public async Task<List<PurchasePayableReceiptDto>> GetLinksAsync(Guid receiptId)
        {
            var links = await _linkAppService.GetListAsync(null, null, receiptId, null);

            return links;
        }

        [HttpGet]
        [Route("purchase-receipts/{receiptId}/count")]
        public async Task<int> GetLinkCountAsync(Guid receiptId)
        {
            var count = await _linkAppService.GetCountAsync(null, null, receiptId, null);

            return count;
        }
    }
}
